/*
 * @lc app=leetcode.cn id=628 lang=java
 *
 * [628] 三个数的最大乘积
 *
 * https://leetcode-cn.com/problems/maximum-product-of-three-numbers/description/
 *
 * algorithms
 * Easy (42.39%)
 * Total Accepted:    4.6K
 * Total Submissions: 10.8K
 * Testcase Example:  '[1,2,3]'
 *
 * 给定一个整型数组，在数组中找出由三个数组成的最大乘积，并输出这个乘积。
 * 
 * 示例 1:
 * 
 * 
 * 输入: [1,2,3]
 * 输出: 6
 * 
 * 
 * 示例 2:
 * 
 * 
 * 输入: [1,2,3,4]
 * 输出: 24
 * 
 * 
 * 注意:
 * 
 * 
 * 给定的整型数组长度范围是[3,10^4]，数组中所有的元素范围是[-1000, 1000]。
 * 输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。
 * 
 * 
 */
class Solution {
    public int maximumProduct(int[] nums) {
        int bigest, bigger, big, smaller, smallest;
        bigest = bigger = big = Integer.MIN_VALUE;
        smaller = smallest = Integer.MAX_VALUE;
        for (int x : nums) {
            if (x > bigest) {
                big = bigger;
                bigger = bigest;
                bigest = x;
            } else if (x > bigger) {
                big = bigger;                
                bigger = x;
            } else if (x > big) {
                big = x;
            }
            if (x < smallest) {
                smaller = smallest;
                smallest = x;
            } else if (x < smaller) {
                smaller = x;
            }
        }
        return Math.max(bigest * bigger * big, bigest * smallest * smaller);
    }
}

